package com.example.nio02;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Date 2022/10/7
 * @Author Jonathan
 */
public class NIODemo02 {
    public static void main(String[] args) {
        try (FileChannel fileChannel = new RandomAccessFile("D:\\code\\example\\netty-learn\\data.txt", "rw").getChannel();) {
            final ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true) {
                final int read = fileChannel.read(buffer);
                if (read == -1) {
                    break;
                }
                // 切换为读模式
                buffer.flip();
                while (buffer.hasRemaining()) {
                    final byte b = buffer.get();
                    System.out.print((char) b);
                }
                buffer.clear();
            }
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
